﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class CodeModify : Form
    {
        public CodeModify()
        {
            InitializeComponent();
        }

        public CodeModify(string jybh)
        {
            InitializeComponent();
            textBox1.Text = jybh;
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox2.Text = string.Empty;
                this.textBox3.Text = string.Empty;
                this.textBox4.Text = string.Empty;
                this.textBox5.Text = string.Empty;
                this.textBox6.Text = string.Empty;
                this.textBox7.Text = string.Empty;

                GetDataFromSqlLite.SourceCode sourcecode = GetDataFromSqlLite.GetSourceCode(textBox1.Text);
                if (sourcecode != null && sourcecode.jysr != null)
                {
                    textBox2.Text = sourcecode.jysr;
                    textBox3.Text = sourcecode.jysc;
                    textBox4.Text = sourcecode.jyname;
                    textBox5.Text = sourcecode.jydesc;
                    textBox6.Text = sourcecode.jysr_param;
                    textBox7.Text = sourcecode.jysc_param;
                }
                else
                {
                    MessageBox.Show("未查询到此接口信息");
                    this.textBox2.Text = @"using Newtonsoft.Json;
using System.Collections.Generic;

public class ClassName
{
    public static string MethodName()
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();

        dict.Add(""mdtrt_cert_type"", ""就诊凭证类型"");
        dict.Add(""mdtrt_cert_no"", ""就诊凭证编号"");
        dict.Add(""card_sn"", ""卡识别码"");
        dict.Add(""begntime"", ""开始时间"");
        dict.Add(""psn_cert_type"", ""人员证件类型"");
        dict.Add(""certno"", ""证件号码"");
        dict.Add(""psn_name"", ""人员姓名"");

        return MethodName(dict);
    }

    public static string MethodName(Dictionary<string, object> dict)
    {
        var data = new
        {
            mdtrt_cert_type = dict[""mdtrt_cert_type""],
            mdtrt_cert_no = dict[""mdtrt_cert_no""],
            card_sn = dict[""card_sn""],
            begntime = dict[""begntime""],
            psn_cert_type = dict[""psn_cert_type""],
            certno = dict[""certno""],
            psn_name = dict[""psn_name""],
        };
        var jsonData = new
        {
            data = data
        };
        return JsonConvert.SerializeObject(jsonData);
    }
}";
                    this.textBox3.Text = @"using System.Collections.Generic;

public class Output
{
    public Baseinfo baseinfo { set; get; }
    public List<Insuinfo> insuinfo { set; get; }
    public List<Idetinfo> idetinfo { set; get; }

    public class Baseinfo
    {
        public string psn_no { set; get; }
        public string psn_cert_type { set; get; }
        public string certno { set; get; }
        public string psn_name { set; get; }
        public string gend { set; get; }
        public string naty { set; get; }
        public string brdy { set; get; }
        public string age { set; get; }
        public Exp_content exp_content { set; get; }

        public class Exp_content
        {
            public string mdtrt_cert_type { set; get; }
            public string mdtrt_cert_no { set; get; }
            public List<Acct_mulaid> acct_mulaid { set; get; }
            public string ectoken { set; get; }

            public class Acct_mulaid
            {
                public string auther_name { set; get; }
                public string auther_insu_admdvs { set; get; }
                public string auther_balc { set; get; }
            }
        }
    }

    public class Insuinfo
    {
        public string balc { set; get; }
        public string insutype { set; get; }
        public string psn_type { set; get; }
        public string psn_insu_stas { set; get; }
        public string psn_insu_date { set; get; }
        public string paus_insu_date { set; get; }
        public string cvlserv_flag { set; get; }
        public string insuplc_admdvs { set; get; }
        public string emp_name { set; get; }
    }

    public class Idetinfo
    {
        public string psn_idet_type { set; get; }
        public string psn_type_lv { set; get; }
        public string memo { set; get; }
        public string begntime { set; get; }
        public string endtime { set; get; }
    }
}
";

                    this.textBox6.Text = @"
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Data;

public class ClassName
{
    public static DataSet MethodName()
    {
        DataSet ds = new DataSet();

        DataTable data = new DataTable(""data"");
        data.Columns.Add(""序号"", typeof(int));
        data.Columns.Add(""参数代码"", typeof(string));
        data.Columns.Add(""参数名称"", typeof(string));
        data.Columns.Add(""参数类型"", typeof(string));
        data.Columns.Add(""参数长度"", typeof(string));
        data.Columns.Add(""代码标识"", typeof(string));
        data.Columns.Add(""是否必填"", typeof(string));
        data.Columns.Add(""说明"", typeof(string));
        data.PrimaryKey = new DataColumn[] { data.Columns[""序号""] };

        DataTable baseinfo = data.Clone();
        baseinfo.TableName = ""基本信息-baseinfo"";
        baseinfo.Rows.Add(1, ""psn_no"", ""人员编号"", ""字符型"", ""30"", """", ""Y"", """");
        baseinfo.Rows.Add(2, ""psn_cert_type"", ""人员证件类型"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        baseinfo.Rows.Add(3, ""certno"", ""证件号码"", ""字符型"", ""50"", """", ""Y"", """");
        baseinfo.Rows.Add(4, ""psn_name"", ""人员姓名"", ""字符型"", ""50"", """", ""Y"", """");
        baseinfo.Rows.Add(5, ""gend"", ""性别"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        baseinfo.Rows.Add(6, ""naty"", ""民族"", ""字符型"", ""3"", ""Y"", """", """");
        baseinfo.Rows.Add(7, ""brdy"", ""出生日期"", ""日期型"", """", """", """", ""yyyy-MM-dd"");
        baseinfo.Rows.Add(8, ""age"", ""年龄"", ""数值型"", ""4,1"", """", ""Y"", """");
        baseinfo.Rows.Add(9, ""exp_content"", ""字段扩展"", ""字符型"", ""4000"", """", """", """");
        ds.Tables.Add(baseinfo);

        DataTable insuinfo = data.Clone();
        insuinfo.TableName = ""参保信息列表-insuinfo"";
        insuinfo.Rows.Add(1, ""balc"", ""余额"", ""数值型"", ""16,2"", """", ""Y"", """");
        insuinfo.Rows.Add(2, ""insutype"", ""险种类型"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        insuinfo.Rows.Add(3, ""psn_type"", ""人员类别"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        insuinfo.Rows.Add(4, ""psn_insu_stas"", ""人员参保状态"", ""字符型"", ""6"", ""Y"", """", """");
        insuinfo.Rows.Add(5, ""psn_insu_date"", ""个人参保日期"", ""日期型"", ""3"", """", """", ""yyyy-MM-dd"");
        insuinfo.Rows.Add(6, ""paus_insu_date"", ""暂停参保日期"", ""日期型"", ""3"", """", """", ""yyyy-MM-dd"");
        insuinfo.Rows.Add(7, ""cvlserv_flag"", ""公务员标志"", ""字符型"", ""3"", ""Y"", ""Y"", """");
        insuinfo.Rows.Add(8, ""insuplc_admdvs"", ""参保地医保区划"", ""字符型"", ""6"", """", ""Y"", """");
        insuinfo.Rows.Add(9, ""emp_name"", ""单位名称"", ""字符型"", ""200"", """", """", """");
        ds.Tables.Add(insuinfo);

        DataTable idetinfo = data.Clone();
        idetinfo.TableName = ""身份信息列表-idetinfo"";
        idetinfo.Rows.Add(1, ""psn_idet_type"", ""人员身份类别"", ""字符型"", ""3"", ""Y"", ""Y"", """");
        idetinfo.Rows.Add(2, ""psn_type_lv"", ""人员类别等级"", ""字符型"", ""3"", ""Y"", """", ""详见残疾等级字典"");
        idetinfo.Rows.Add(3, ""memo"", ""备注"", ""字符型"", ""500"", """", """", """");
        idetinfo.Rows.Add(4, ""begntime"", ""开始时间"", ""日期时间型"", """", ""Y"", """", ""yyyy-MM-dd"");
        idetinfo.Rows.Add(5, ""endtime"", ""结束时间"", ""日期时间型"", """", """", """", ""yyyy-MM-dd"");
        ds.Tables.Add(idetinfo);

        return ds;
    }
}";
                    this.textBox7.Text = @"
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Data;

public class ClassName
{
    public static DataSet MethodName()
    {
        DataSet ds = new DataSet();

        DataTable data = new DataTable(""data"");
        data.Columns.Add(""序号"", typeof(int));
        data.Columns.Add(""参数代码"", typeof(string));
        data.Columns.Add(""参数名称"", typeof(string));
        data.Columns.Add(""参数类型"", typeof(string));
        data.Columns.Add(""参数长度"", typeof(string));
        data.Columns.Add(""代码标识"", typeof(string));
        data.Columns.Add(""是否必填"", typeof(string));
        data.Columns.Add(""说明"", typeof(string));
        data.PrimaryKey = new DataColumn[] { data.Columns[""序号""] };

        DataTable baseinfo = data.Clone();
        baseinfo.TableName = ""基本信息-baseinfo"";
        baseinfo.Rows.Add(1, ""psn_no"", ""人员编号"", ""字符型"", ""30"", """", ""Y"", """");
        baseinfo.Rows.Add(2, ""psn_cert_type"", ""人员证件类型"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        baseinfo.Rows.Add(3, ""certno"", ""证件号码"", ""字符型"", ""50"", """", ""Y"", """");
        baseinfo.Rows.Add(4, ""psn_name"", ""人员姓名"", ""字符型"", ""50"", """", ""Y"", """");
        baseinfo.Rows.Add(5, ""gend"", ""性别"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        baseinfo.Rows.Add(6, ""naty"", ""民族"", ""字符型"", ""3"", ""Y"", """", """");
        baseinfo.Rows.Add(7, ""brdy"", ""出生日期"", ""日期型"", """", """", """", ""yyyy-MM-dd"");
        baseinfo.Rows.Add(8, ""age"", ""年龄"", ""数值型"", ""4,1"", """", ""Y"", """");
        baseinfo.Rows.Add(9, ""exp_content"", ""字段扩展"", ""字符型"", ""4000"", """", """", """");
        ds.Tables.Add(baseinfo);

        DataTable insuinfo = data.Clone();
        insuinfo.TableName = ""参保信息列表-insuinfo"";
        insuinfo.Rows.Add(1, ""balc"", ""余额"", ""数值型"", ""16,2"", """", ""Y"", """");
        insuinfo.Rows.Add(2, ""insutype"", ""险种类型"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        insuinfo.Rows.Add(3, ""psn_type"", ""人员类别"", ""字符型"", ""6"", ""Y"", ""Y"", """");
        insuinfo.Rows.Add(4, ""psn_insu_stas"", ""人员参保状态"", ""字符型"", ""6"", ""Y"", """", """");
        insuinfo.Rows.Add(5, ""psn_insu_date"", ""个人参保日期"", ""日期型"", ""3"", """", """", ""yyyy-MM-dd"");
        insuinfo.Rows.Add(6, ""paus_insu_date"", ""暂停参保日期"", ""日期型"", ""3"", """", """", ""yyyy-MM-dd"");
        insuinfo.Rows.Add(7, ""cvlserv_flag"", ""公务员标志"", ""字符型"", ""3"", ""Y"", ""Y"", """");
        insuinfo.Rows.Add(8, ""insuplc_admdvs"", ""参保地医保区划"", ""字符型"", ""6"", """", ""Y"", """");
        insuinfo.Rows.Add(9, ""emp_name"", ""单位名称"", ""字符型"", ""200"", """", """", """");
        ds.Tables.Add(insuinfo);

        DataTable idetinfo = data.Clone();
        idetinfo.TableName = ""身份信息列表-idetinfo"";
        idetinfo.Rows.Add(1, ""psn_idet_type"", ""人员身份类别"", ""字符型"", ""3"", ""Y"", ""Y"", """");
        idetinfo.Rows.Add(2, ""psn_type_lv"", ""人员类别等级"", ""字符型"", ""3"", ""Y"", """", ""详见残疾等级字典"");
        idetinfo.Rows.Add(3, ""memo"", ""备注"", ""字符型"", ""500"", """", """", """");
        idetinfo.Rows.Add(4, ""begntime"", ""开始时间"", ""日期时间型"", """", ""Y"", """", ""yyyy-MM-dd"");
        idetinfo.Rows.Add(5, ""endtime"", ""结束时间"", ""日期时间型"", """", """", """", ""yyyy-MM-dd"");
        ds.Tables.Add(idetinfo);

        return ds;
    }
}";
                }
            }

        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            string jysr = this.textBox2.Text;
            if ("".Equals(jysr))
            {
                MessageBox.Show("无内容");
                return;
            }

            string assemble_error = DynamicLoader.CheckAssembly(jysr);
            if (!"".Equals(assemble_error))
            {
                MessageBox.Show("编译出错:" + assemble_error);
                return;
            }
            else
            {
                MessageBox.Show("编译成功");
            }
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            string jysr = this.textBox2.Text;
            if (jysr == "")
            {
                MessageBox.Show("无内容");
                return;
            }
            List<string> methodinfo = DynamicLoader.getMethodInfo(jysr);
            StringBuilder stringBuilder = new StringBuilder("");
            foreach (string sss in methodinfo)
            {
                stringBuilder.Append(sss);
                stringBuilder.Append("\n");
            }
            MessageBox.Show(stringBuilder.ToString());

            string str = DynamicLoader.CheckMethod(jysr, null, null).ToString();
            if (!"".Equals(str))
            {
                MessageBox.Show(str);
                return;
            }
            MessageBox.Show(DynamicLoader.getMethod(jysr, null, null).ToString());
        }


        public static string FormatCode(string sourceCode)
        {
            // 创建带语言服务的 MEF 主机
            var host = MefHostServices.Create(MefHostServices.DefaultAssemblies);
            var workspace = new AdhocWorkspace(host);

            // 解析源代码
            var syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);
            var root = syntaxTree.GetRoot();

            // 格式化代码
            var formattedRoot = Formatter.Format(root, workspace);
            return formattedRoot.ToFullString();
        }

        private  void button5_Click(object sender, System.EventArgs e)
        {
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("无内容");
                return;
            }
            this.textBox2.Text = FormatCode(this.textBox2.Text);
            MessageBox.Show("格式化完毕");
        }

        private void button4_Click(object sender, System.EventArgs e)
        {
            string jysc = this.textBox3.Text;
            if ("".Equals(jysc))
            {
                MessageBox.Show("无内容");
                return;
            }

            this.textBox3.Text = FormatCode(this.textBox3.Text);

            string assemble_error = DynamicLoader.CheckAssembly(jysc);
            if (!"".Equals(assemble_error))
            {
                MessageBox.Show("编译出错:" + assemble_error);
                return;
            }
            else
            {
                MessageBox.Show("编译成功");
            }
        }

        private  void button6_Click(object sender, System.EventArgs e)
        {

            if (this.textBox3.Text == "")
            {
                MessageBox.Show("无内容");
                return;
            }
            this.textBox3.Text = FormatCode(this.textBox3.Text);
            MessageBox.Show("格式化完毕");
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            string code = this.textBox1.Text;
            string jysr = this.textBox2.Text;
            string jysc = this.textBox3.Text;
            string jyname = this.textBox4.Text;
            string jydesc = this.textBox5.Text;
            string jysr_param = this.textBox6.Text;
            string jysc_param = this.textBox7.Text;

            if ("".Equals(code) || "".Equals(jysr))
            {
                MessageBox.Show("无内容");
                return;
            }

            string assemble_error = DynamicLoader.CheckAssembly(jysr);
            if (!"".Equals(assemble_error))
            {
                MessageBox.Show("编译出错:" + assemble_error);
                return;
            }

            bool flag = GetDataFromSqlLite.SetSourceCode(code, jysr, jysc,jyname,jydesc, jysr_param, jysc_param);
            if (flag)
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string jysr = this.textBox6.Text;

            // 格式化
            this.textBox6.Text = FormatCode(jysr);

            // 编译
            string assemble_error = DynamicLoader.CheckAssembly(jysr);
            if (!"".Equals(assemble_error))
            {
                MessageBox.Show("编译出错:" + assemble_error);
                return;
            }

            // 描述
            try
            {
                DataSet ds = (DataSet)DynamicLoader.getMethod(jysr,  null, null);
                foreach (DataTable table in ds.Tables)
                {
                    new Codedesc(table).Show();
                }
            }
            catch { }
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            string jysc = this.textBox7.Text;

            // 格式化
            this.textBox7.Text = FormatCode(jysc);

            // 编译
            string assemble_error = DynamicLoader.CheckAssembly(jysc);
            if (!"".Equals(assemble_error))
            {
                MessageBox.Show("编译出错:" + assemble_error);
                return;
            }

            // 描述
            try
            {
                DataSet ds = (DataSet)DynamicLoader.getMethod(jysc, null, null);
                foreach (DataTable table in ds.Tables)
                {
                    new Codedesc(table).Show();
                }
            }
            catch { }
        }
    }
}
